﻿using client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WSClient
{
    public partial class ClientForm : Form
    {
        private WebSocketClient client;

        public ClientForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 收到服务器端消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Client_OnMessage(object sender, WebSocketClient.MessageEventArgs<string> e)
        {
            txtRec.Invoke(new Action(() =>
            {
                txtRec.Text = e.Message + "\r\n" + txtRec.Text;
            }));
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            client.SendMessageAsync(txtSend.Text);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            client = new WebSocketClient(txtUrl.Text);
            client.ConnectAsync();
            client.OnMessage += Client_OnMessage;
            btnSend.Enabled = true;
        }
    }
}
